//
//  GenderEditViewController.swift
//  App_Profile
//
//  Created by 蒋晨成 on 2022/2/11.
//

import Adaptor
import App_Common
import Center
import RxCocoa
import RxSwift
import SnapKit
import Toast
import UICore
import UIKit

class SignatureEditViewController: ViewController {
    let maxSignatureLength = 20

    lazy var contentView: UIView = .init()
        .x
        .backgroundColor(Adaptor.Profile.EditDetail.textFiledBackgroundColor)
        .instance

    lazy var signatueTextView: UITextView = .init()
        .x
        .backgroundColor(Adaptor.Profile.EditDetail.textFiledBackgroundColor)
        .text(UserCenter.userInfo?.signature ?? "")
        .returnKeyType(.done)
        .delegate(self)
        .font(.h3)
        .instance

    lazy var countLabel: UILabel = .init()
        .x
        .textColor(Adaptor.Profile.subtitleColor)
        .font(.root)
        .instance

    lazy var disposeBag = DisposeBag()
}

// MARK: - Override

extension SignatureEditViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        setupNav()
        setup()
        bind()
    }
}

private extension SignatureEditViewController {
    func setup() {
        view.backgroundColor = Adaptor.Profile.background

        contentView.x.add(to: view).snp.makeConstraints {
            $0.leading.trailing.equalToSuperview()
            $0.height.equalTo(168)
            $0.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(20)
        }

        signatueTextView.x.add(to: contentView).snp.makeConstraints {
            $0.edges.equalTo(UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20))
        }

        signatueTextView.becomeFirstResponder()

        countLabel.x.add(to: view).snp.makeConstraints {
            $0.top.equalTo(contentView.snp.bottom).offset(6)
            $0.trailing.equalToSuperview().offset(-24)
        }
    }

    func setupNav() {
        if (navigationController?.viewControllers.count ?? 0) > 1 {
            let backImage = ProfileModular.image(named: "Common.Nav.Action.Back")
            addDefaultLeftBarButtonItem(image: backImage)
        }

        let saveItem = UIBarButtonItem(
            title: ProfileModular.localizedString(key: "Profile.Userinfo.Save"),
            style: .plain,
            target: self,
            action: #selector(confirm)
        )
        navigationItem.rightBarButtonItem = saveItem

        setNavigationBarTintColor(tintColor: Adaptor.Profile.titleColor)
        navigationItem.title = ProfileModular.localizedString(key: "Profile.Userinfo.Signature")
    }
}

// MARK: - Routable

extension SignatureEditViewController: Routable {
    static func initialize(values _: [String: Any]) -> ViewController? {
        SignatureEditViewController(nibName: nil, bundle: nil)
    }

    static var routePattern = "profile/edit/signature"

    static let description: String = "Profile Edit Signature Router"
}
